#***************************************************************************
#                                  _   _ ____  _
#  Project                     ___| | | |  _ \| |
#                             / __| | | | |_) | |
#                            | (__| |_| |  _ <| |___
#                             \___|\___/|_| \_\_____|
#
# Copyright (C) Daniel Stenberg, <daniel@haxx.se>, et al.
#
# This software is licensed as described in the file COPYING, which
# you should have received as part of this distribution. The terms
# are also available at https://curl.se/docs/copyright.html.
#
# You may opt to use, copy, modify, merge, publish, distribute and/or sell
# copies of the Software, and permit persons to whom the Software is
# furnished to do so, under the terms of the COPYING file.
#
# This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
# KIND, either express or implied.
#
# SPDX-License-Identifier: curl
#
###########################################################################
# 初始化 CMake 包配置
@PACKAGE_INIT@

# 包含 CMake 查找依赖宏模块，用于查找依赖项
include(CMakeFindDependencyMacro)

# 若使用 OpenSSL 库
if("@USE_OPENSSL@")
  # 查找指定版本的 OpenSSL 依赖
  find_dependency(OpenSSL "@OPENSSL_VERSION_MAJOR@")
endif()

# 若支持 ZLIB 库
if("@HAVE_LIBZ@")
  # 查找指定版本的 ZLIB 依赖
  find_dependency(ZLIB "@ZLIB_VERSION_MAJOR@")
endif()

# 包含导出目标的 CMake 配置文件
include("${CMAKE_CURRENT_LIST_DIR}/@TARGETS_EXPORT_NAME@.cmake")

# 为共享库或静态库创建别名
if(NOT TARGET @PROJECT_NAME@::libcurl)
  # 添加 @PROJECT_NAME@::libcurl 别名，指向 @PROJECT_NAME@::@LIB_SELECTED@
  add_library(@PROJECT_NAME@::libcurl ALIAS @PROJECT_NAME@::@LIB_SELECTED@)
endif()

# 为了与 CMake 的 FindCURL.cmake 兼容
# 设置 CURL 版本字符串变量
set(CURL_VERSION_STRING "@CURLVERSION@")
# 设置 CURL 库变量，指向 @PROJECT_NAME@::libcurl
set(CURL_LIBRARIES @PROJECT_NAME@::libcurl)
# 设置并检查 CURL 头文件目录变量
set_and_check(CURL_INCLUDE_DIRS "@PACKAGE_CMAKE_INSTALL_INCLUDEDIR@")

# 设置 CURL 支持的协议列表变量
set(CURL_SUPPORTED_PROTOCOLS "@CURL_SUPPORTED_PROTOCOLS_LIST@")
# 设置 CURL 支持的特性列表变量
set(CURL_SUPPORTED_FEATURES "@CURL_SUPPORTED_FEATURES_LIST@")

# 遍历 CURL 支持的协议和特性列表
foreach(_item IN LISTS CURL_SUPPORTED_PROTOCOLS CURL_SUPPORTED_FEATURES)
  # 为每个支持的协议和特性设置对应的变量为 TRUE
  set(CURL_SUPPORTS_${_item} TRUE)
endforeach()

# 初始化缺失的必需组件变量
set(_missing_req "")
# 遍历用户查找的 CURL 组件列表
foreach(_item IN LISTS CURL_FIND_COMPONENTS)
  # 若 CURL 支持该组件
  if(CURL_SUPPORTS_${_item})
    # 设置该组件已找到的变量为 TRUE
    set(CURL_${_item}_FOUND TRUE)
  # 若用户要求必需该组件但 CURL 不支持
  elseif(CURL_FIND_REQUIRED_${_item})
    # 将该组件添加到缺失的必需组件列表中
    list(APPEND _missing_req ${_item})
  endif()
endforeach()

# 若存在缺失的必需组件
if(_missing_req)
  # 将缺失的必需组件列表中的分号替换为空格
  string(REPLACE ";" " " _missing_req "${_missing_req}")
  # 若用户要求必需找到所有组件
  if(CURL_FIND_REQUIRED)
    # 输出致命错误信息，提示缺失的必需组件
    message(FATAL_ERROR "CURL: missing required components: ${_missing_req}")
  endif()
  # 移除缺失的必需组件变量
  unset(_missing_req)
endif()

# 检查必需组件是否都已找到
check_required_components("@PROJECT_NAME@")
